/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package loganalysis;

import java.awt.Font;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYTextAnnotation;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import java.io.*;
import java.util.*;
/**
 * A demonstration application showing how to create a vertical combined chart.
 *
 */
public class CombinedXYPlot extends ApplicationFrame {

    /**
     * Constructs a new demonstration application.
     *
     * @param title  the frame title.
     */
    public CombinedXYPlot(final String title) {

        super(title);
        this.ScanData("falkon_summary.txt");
       System.out.println("Start Curration");
        this.Curration();
        final JFreeChart chart = createCombinedChart();
        final ChartPanel panel = new ChartPanel(chart, true, true, true, false, true);
        panel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(panel);

    }
 
     private ArrayList<ArrayList<String>> intermediateData ;
    /**
     * Creates a combined chart.
     *
     * @return the combined chart.
     */
    private JFreeChart createCombinedChart() {

        // create subplot 1...
        final XYDataset data1 = createDataset1();
        final XYItemRenderer renderer1 = new StandardXYItemRenderer();
        final NumberAxis rangeAxis1 = new NumberAxis(" ");
        final XYPlot subplot1 = new XYPlot(data1, null, rangeAxis1, renderer1);
        subplot1.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
        
        final XYTextAnnotation annotation = new XYTextAnnotation("", 50.0, 10000.0);
        annotation.setFont(new Font("SansSerif", Font.PLAIN, 9));
        annotation.setRotationAngle(Math.PI / 4.0);
        subplot1.addAnnotation(annotation);
        
        // create subplot 2...
        final XYDataset data2 = createDataset2();
        final XYItemRenderer renderer2 = new StandardXYItemRenderer();
        final NumberAxis rangeAxis2 = new NumberAxis(" Free Workers ");
        rangeAxis2.setAutoRangeIncludesZero(false);
        final XYPlot subplot2 = new XYPlot(data2, null, rangeAxis2, renderer2);
        subplot2.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);

        // parent plot...
        final CombinedDomainXYPlot plot = new CombinedDomainXYPlot(new NumberAxis("Milliseconds"));
        plot.setGap(10.0);
        
        // add the subplots...
        plot.add(subplot1, 1);
        plot.add(subplot2, 1);
        plot.setOrientation(PlotOrientation.VERTICAL);

        // return a new chart containing the overlaid plot...
        return new JFreeChart("BGP-Interpid",JFreeChart.DEFAULT_TITLE_FONT, plot, true);

    }

    /**
     * Creates a sample dataset.
     *
     * @return Series 1.
     */
    private XYDataset createDataset1() {

        // create dataset 1...
        final XYSeries series1 = new XYSeries("Wait Queue Length");
       for (int i =0; i<intermediateData.size();i++)
       {
          series1.add(Double.parseDouble(intermediateData.get(i).get(0)),Double.parseDouble(intermediateData.get(i).get(10))); 
       }
        
        

        final XYSeries series2 = new XYSeries("Delievered Task");
        for (int i =0; i<intermediateData.size();i++)
       {
          series2.add(Double.parseDouble(intermediateData.get(i).get(0)),Double.parseDouble(intermediateData.get(i).get(12))); 
       }
        final XYSeriesCollection collection = new XYSeriesCollection();
        collection.addSeries(series1);
        collection.addSeries(series2);
        return collection;

    }

    /**
     * Creates a sample dataset.
     *
     * @return Series 2.
     */
    private XYDataset createDataset2() {

        // create dataset 2...
        final XYSeries series2 = new XYSeries("Number of Free Workers");

        for (int i =0; i<intermediateData.size();i++)
       {
          series2.add(Double.parseDouble(intermediateData.get(i).get(0)),Double.parseDouble(intermediateData.get(i).get(5))); 
       }

        return new XYSeriesCollection(series2);

    }

   /* Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
    public static void main(final String[] args) {

        final CombinedXYPlot demo = new CombinedXYPlot("BGP-Interpid-Summary Log");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }
    
    public void ScanData(String filePath)
       {
            String fullFilePath = filePath;
            String line ="";
            BufferedReader loadLogFile;
            //2D Array
         intermediateData = new ArrayList<ArrayList<String>>();
             //Create file object and read it 
            try {
            loadLogFile= new BufferedReader(new FileReader (fullFilePath));
                
            
        //Initialize Row and column counter
         int row = 0;
         int col = 0;
         //Nested Loops to read file 
         
         while( (line = loadLogFile.readLine())!=null)
         {
             Scanner scanLine = new Scanner(line);
             intermediateData.add(new ArrayList<String>());
             while(scanLine.hasNext())
             {
                 intermediateData.get(row).add(scanLine.next());
                 col++;
             }
             row++;
         }
         }
         catch(FileNotFoundException e)
         {
             System.err.println("Error in reading logs: "+e);
         }
      catch(IOException e)
                {
                    System.err.println("Error in finding log file: "+e);
                }
            
            intermediateData.remove(0);
            intermediateData.remove(0);
        // Tesing region 
//        Iterator list = intermediateData.iterator();
//        while(list.hasNext())
//        {
//            System.out.println(list.next());
//        } 
       } 

public void Curration()
    {
        
       int cured =0;
       int k = intermediateData.size();
       ArrayList<String> remove;
       for (int i =0;i<k;i++)
       {    
          //System.out.println("Row is "+ i);
         //  System.out.println(logData.get(i));
             try{
               
               if(intermediateData.get(i).isEmpty())
                        {
                        //call code to curate the tmp file
                       System.out.println("Code is here");
                       
                      // cured++;
                        }
                   
                   }catch(IndexOutOfBoundsException e)
                   {
                       intermediateData.remove(i);
                   System.out.println("Error in line " + e);
                   System.out.println("Index of Row "+i);
                   intermediateData.remove(i);
                   cured++;
                   }
       }
       System.out.println("Total Lines Cured "+cured);
    
    }

}